package struts.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class CharacterEncoding implements Filter {
	private String encoding=null;
	@Override
	public void init(FilterConfig filterconfig) throws ServletException {
		encoding=filterconfig.getInitParameter("encoding");

	}

	@Override
	public void doFilter(ServletRequest request,ServletResponse response, FilterChain filterchain)			throws IOException, ServletException {
		request.setCharacterEncoding(encoding);
		HttpServletRequest httpRequest=(HttpServletRequest)request;
		httpRequest.getSession().setAttribute("INVOKE_ORIGINAL_URL", httpRequest.getServletPath());
		response.setCharacterEncoding(encoding);
		filterchain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		encoding=null;
	}


}
